
function getAllUsers(){
	showLoader();
	var parametersData = {
			 "action": "user_list"
			 };
	 
	$.getJSON('process.php',parametersData, function(data,textStatus) {
		$('#user_list').html("");
         $.each(data, function(key, val) {
        	 var resultLine='<tr><td>'+val.id+'</td><td>'+val.first_name+'</td><td>'+val.last_name+'</td><td>'+val.email+'</td><td>'+val.role+'</td><td><a href="#" data-id="'+val.id+'" id="link_edit_user" data-toggle="modal" data-target="#user-form-edit-modal"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a> <a href="#" data-id="'+val.id+'" id="link_remove_user" data-toggle="modal" data-target="#user-remove-modal"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td></tr>';
        	 $('#user_list').append(resultLine);
         });
         hideLoader();
	 });
	
}

function getAllPhases(){
	var parametersData = {
			 "action": "phase_list"
			 };
	$.getJSON('process.php',parametersData, function(data,textStatus) {
		$('#phases_list').html("");
         $.each(data, function(key, val) {
        	 var resultLine='<div class="phase-list-outer">';
        	 resultLine+='<div id="div_phase_'+val.id+'" class="phase-list-inline-left"><strong>'+val.phase_name+'</strong></div>';
        	 resultLine+='<div class="phase-list-inline-right"><a href="#" data-id="'+val.id+'" id="link_down_phase_'+val.id+'" data-toggle="modal" data-target=""><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></a> <a href="#" data-id="'+val.id+'" id="link_down_phase_'+val.id+'" data-toggle="modal" data-target="#non"><span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span></a> <a href="#" data-id="'+val.id+'" id="link_edit_phase_'+val.id+'" data-toggle="modal" data-target="#non"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a> <a href="#" data-id="'+val.id+'" id="link_remove_phase_'+val.id+'" data-toggle="modal" data-target="#phase-remove-modal"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></div>';
        	 resultLine+='</div>';
        	 $('#phases_list').append(resultLine);
         });
	 });
}

function showLoader(){
	$("html").addClass( "loading" );
}

function hideLoader(){
	$("html").removeClass( "loading" );
}

function searchUsers(pattern){
	
}

$(document).ready(function() {
	// new user
	$("#user-add-form").submit(function(event) {
		$("#user-add-failed").hide();
		$("#user-add-success").hide();
		$("#user-add-loader").show();
		var userFirstName= $("#userFirstName").val();
		var userLastName= $("#userFirstName").val();
		var userEmail= $("#userEmail").val();
		var userAdmin= $("#userAdmin").is(':checked');
		var parametersData = {
				 "action": "user_add",
				 "userFirstName": userFirstName,
				 "userLastName": userLastName,
				 "userEmail": userEmail,
				 "userAdmin": userAdmin
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			$("#user-add-loader").hide();
			$("#user-add-failed").hide();
			$("#user-add-success").hide();
			$("#user-add-success").show();
			
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#user-add-loader").hide();
			$("#user-add-success").hide();
			$("#user-add-hide").show();
			$("#user-add-failed").show();
		});
		$("#user-add-loader").hide();
		getAllUsers();
		event.preventDefault();
	});
	
	// delete phase
	$("#phase_delete_confirm_button").click(function() {
		$("#phase-remove-loader").show();
		var phaseId=$('#phaseToDeleteId').val();
		var parametersData = {
				 "action": "phase_delete",
				 "id": phaseId
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});
		
		
		request.done(function( returnedData ) {
			$("#phase-remove-message").hide();
			$("#phase-remove-loader").hide();
			$("#phase-remove-failed").hide();
			$("#phase-remove-success").hide();
			$("#phase-remove-success").show();
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#phase-remove-message").hide();
			$("#phase-remove-loader").hide();
			$("#phase-remove-success").hide();
			$("#phase-remove-failed").hide();
			$("#phase-remove-failed").show();
		});
		$("#phase-remove-loader").hide();
		getAllPhases();
	});
	
	// change the phaseToDeleteId value when delete icon is clicked
	$('#phase-remove-modal').on('show.bs.modal', function (event) {
		$("#phase-remove-message").show();
		$("#phase-remove-success").hide();
		$("#phase-remove-failed").hide();
		var uiTarget = $(event.relatedTarget);
		var id = uiTarget.data('id');
		$('#phaseToDeleteId').val(id);
	});
	
	// delete user
	$("#user_delete_confirm_button").click(function() {
		$("#user-remove-loader").show();
		var userId=$('#userToDeleteId').val();
		var parametersData = {
				 "action": "user_delete",
				 "id": userId
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			$("#user-remove-message").hide();
			$("#user-remove-loader").hide();
			$("#user-remove-failed").hide();
			$("#user-remove-success").hide();
			$("#user-remove-success").show();
			
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#user-remove-message").hide();
			$("#user-remove-loader").hide();
			$("#user-remove-success").hide();
			$("#user-remove-failed").hide();
			$("#user-remove-failed").show();
		});
		$("#user-remove-loader").hide();
		getAllUsers();
	});
	

	
	// change the userToDeleteId value when delete icon is clicked
	$('#user-remove-modal').on('show.bs.modal', function (event) {
		$("#user-remove-message").show();
		$("#user-remove-success").hide();
		$("#user-remove-failed").hide();
		var uiTarget = $(event.relatedTarget);
		var id = uiTarget.data('id');
		$('#userToDeleteId').val(id);
	});
	
	// change the user form edit values when edit icon is clicked
	$('#user-form-edit-modal').on('show.bs.modal', function (event) {
		$("#user-edit-success").hide();
		$("#user-edit-failed").hide();
		var uiTarget = $(event.relatedTarget);
		var id = uiTarget.data('id');
		$('#userToEditId').val(id);
		var parametersData = {
				 "action": "get_user",
				 "id": id
				 };
		$.getJSON('process.php',parametersData, function(data,textStatus) {
	        $.each(data, function(key, val) {
	        	 $('#userEditFirstName').val(val.first_name);
	        	 $('#userEditLastName').val(val.last_name);
	        	 $('#userEditEmail').val(val.email);
	        	 if(val.role==1) $('#userEditAdmin').attr('checked', true);
	         });
		 });
		
	});
	
	// edit user clic button
	$("#user-edit-form").submit(function(event) {
		$("#user-edit-loader").show();
		var userId=$('#userToEditId').val();
		var parametersData = {
				 "action": "user_edit",
				 "id": userId,
				 "userEditFirstName": $('#userEditFirstName').val(),
				 "userEditLastName": $('#userEditLastName').val(),
				 "userEditEmail": $('#userEditEmail').val(),
				 "userEditPassword": $('#userEditPassword').val(),
				 "userEditAdmin": ($('#userEditAdmin').is(':checked')==true)?1:0
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			$("#user-edit-loader").hide();
			$("#user-edit-failed").hide();
			$("#user-edit-success").hide();
			$("#user-edit-success").show();
			
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#user-edit-loader").hide();
			$("#user-edit-success").hide();
			$("#user-edit-hide").show();
			$("#user-edit-failed").show();
		});
		$("#user-edit-loader").hide();
		getAllUsers();
		event.preventDefault();
	});
	
	// change the user form edit value when edit profile icon is clicked
	$('#user-form-myprofile-edit-modal').on('show.bs.modal', function (event) {
		$("#user-myprofile-edit-success").hide();
		$("#user-myprofile-edit-failed").hide();
		var uiTarget = $(event.relatedTarget);
		var id = uiTarget.data('id');
		$('#userProfileToEditId').val(id);
		var parametersData = {
				 "action": "get_user",
				 "id": id
				 };
		$.getJSON('process.php',parametersData, function(data,textStatus) {
	        $.each(data, function(key, val) {
	        	 $('#userProfileEditFirstName').val(val.first_name);
	        	 $('#userProfileEditLastName').val(val.last_name);
	        	 $('#userProfileEditEmail').val(val.email);
	        	 if(val.role==1) $('#userProfileEditAdmin').attr('checked', true);
	         });
		 });
		
	});
	
	// edit user clic button
	$("#user-myprofile-edit-form").submit(function(event) {
		$("#user-myprofile-edit-loader").show();
		if($('#userProfilePassword').val() != $('#userProfilePasswordConfirm').val()){
			$("#user-myprofile-edit-loader").hide();
			$("#user-myprofile-edit-password-confirm").show();
			return false;
		}
		var userId=$('#userProfileToEditId').val();
		var parametersData = {
				 "action": "profile_edit",
				 "id": userId,
				 "userProfilePassword": $('#userProfilePassword').val(),
				 "userProfilePasswordConfirm": $('#userProfilePasswordConfirm').val()
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			$("#user-myprofile-edit-password-confirm").hide();
			$("#user-myprofile-edit-loader").hide();
			$("#user-myprofile-edit-failed").hide();
			$("#user-myprofile-edit-success").hide();
			$("#user-myprofile-edit-success").show();
			
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#user-myprofile-edit-password-confirm").hide();
			$("#user-myprofile-edit-loader").hide();
			$("#user-myprofile-edit-success").hide();
			$("#user-myprofile-edit-hide").show();
			$("#user-myprofile-edit-failed").show();
		});
		$("#user-myprofile-edit-loader").hide();
		getAllUsers();
		event.preventDefault();
	});
	
	// phase add form submit
	$("#phase-add-form").submit(function(event) {
		$("#phase-add-success").hide();
		$("#phase-add-failed").hide();
		$("#phases-loader").show();
		var phaseName = $('#phaseName').val();
		var parametersData = {
				 "action": "phase_add",
				 "phase_name": phaseName
				 };
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			$("#phase-add-success").show()
			getAllPhases();
		});
		
		request.fail(function( jqXHR, textStatus ) {
			$("#phase-add-failed").show();
		});
		$("#phases-loader").hide();
		event.preventDefault();
	});
	
	$('#phases-modal').on('show.bs.modal', function (event) {
		$("#phase-add-success").hide();
		$("#phase-add-failed").hide();
		getAllPhases();
	});
	
	// phase name field change
	$( "#phase-name-input" ).change(function() {
		var newValue = $("#phase-name-input").val();
		var phaseId= $("#phase-name-input").data("phaseId");
		
		var parametersData = {
				 "action": "phase_edit_name",
				 "phaseId": phaseId,
				 "newValue": newValue
				 };
		
		var request = $.ajax({
			  url: "process.php",
			  type: "POST",
			  data: parametersData
			});

		request.done(function( returnedData ) {
			// hide text field and show label, apply success class
		});
		
		request.fail(function( jqXHR, textStatus ) {
			// apply error class
		});
		
		getAllPhases();
	});
});
